package ro.utcn.tp.taskManagement.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import ro.utcn.tp.taskManagement.model.Task;

public class ExcelExporter {

	private static WritableCellFormat arialBold;
	private static WritableCellFormat arial;

	public static void exportToExcel(String excelOutputFile, List<Task> tasks) throws IOException, WriteException {
		File file = new File(excelOutputFile);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

		WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
		workbook.createSheet("Export", 0);
		WritableSheet excelSheet = workbook.getSheet(0);
		createHeaders(excelSheet);
		createContent(excelSheet,tasks);

		workbook.write();
		workbook.close();
	}

	private static void createHeaders(WritableSheet sheet) throws WriteException {
		// Lets create a times font
		WritableFont arial10pt = new WritableFont(WritableFont.ARIAL, 10);
		// Define the cell format
		arial = new WritableCellFormat(arial10pt);
		// Lets automatically wrap the cells
		arial.setWrap(true);

		// Create a bold font
		WritableFont arial10ptBold = new WritableFont(WritableFont.ARIAL, 10,
				WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
		arialBold = new WritableCellFormat(arial10ptBold);
		// Lets automatically wrap the cells
		arialBold.setWrap(true);

		CellView cv = new CellView();
		cv.setFormat(arial);
		cv.setFormat(arialBold);
		cv.setAutosize(true);

		// Write headers
		addCaption(sheet, 0, 0, "Description");
		addCaption(sheet, 1, 0, "Status");
		addCaption(sheet, 2, 0, "Schedule date");
		addCaption(sheet, 3, 0, "Closed Date");
	}

	private static void createContent(WritableSheet sheet, List<Task> tasks) throws WriteException,
			RowsExceededException {
		// read translations from language files
		TreeMap<String, HashMap<String, String>> translationsMap = new TreeMap<String, HashMap<String, String>>();

		Iterator<Task> tasksIterator = tasks.iterator();

		int lineIndex = 0;
		while (tasksIterator.hasNext()) {
			Task task = tasksIterator.next();
			lineIndex++;
			addLabel(sheet, 0, lineIndex, task.getDescription());
			addLabel(sheet, 1, lineIndex, task.getStatus());
			
			SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
			String scheduleDate = sdf.format(task.getScheduleDate().getTime());
			String closedDate = "";
			if (task.getClosedDate() != null) {
				closedDate = sdf.format(task.getClosedDate().getTime());
			}
			addLabel(sheet, 2, lineIndex, scheduleDate);
			addLabel(sheet, 3, lineIndex, closedDate);
		}
	}

	private static void addCaption(WritableSheet sheet, int column, int row, String s)
			throws RowsExceededException, WriteException {
		Label label;
		label = new Label(column, row, s, arialBold);
		sheet.addCell(label);
	}

	private static void addLabel(WritableSheet sheet, int column, int row, String s)
			throws WriteException, RowsExceededException {
		Label label;
		label = new Label(column, row, s, arial);
		sheet.addCell(label);
	}
}